package threadtest;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author shentianyu
 * @date 2021-06-29 13:52
 */
public class TeacherExam implements Runnable {
    @Override
    public void run() {
        System.out.println("Exam Start");

        CountDownLatch studentCount = new CountDownLatch(5);

        Semaphore desk = new Semaphore(2);

        long start = System.currentTimeMillis();

        StudentExam studentExam1 = new StudentExam(1000,studentCount,desk);
        StudentExam studentExam2 = new StudentExam(2000,studentCount,desk);
        StudentExam studentExam3 = new StudentExam(3000,studentCount,desk);
        StudentExam studentExam4 = new StudentExam(4000,studentCount,desk);
        StudentExam studentExam5 = new StudentExam(5000,studentCount,desk);

        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(studentExam1);
        executorService.submit(studentExam2);
        executorService.submit(studentExam3);
        executorService.submit(studentExam4);
        executorService.submit(studentExam5);

        try {
            studentCount.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Exam Finish");

        long end = System.currentTimeMillis();

        System.out.println("考试时长"+(end-start));

        executorService.shutdown();
    }
}

class Demo{
    public static void main(String[] args) {
        new Thread(new TeacherExam()).start();
    }
}
